package com.liuxinlong.jobs;

import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.enums.DifficultyStatusEnum;
import com.liuxinlong.modules.dao.DifficultyDao;
import com.liuxinlong.modules.dao.ExpertDao;
import com.liuxinlong.modules.dao.ExpertProjectDao;
import com.liuxinlong.modules.entity.Difficulty;
import com.liuxinlong.modules.entity.Expert;
import com.liuxinlong.modules.entity.ExpertProject;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 使用quartz框架
 */
@Service
@Component("checkIssueTimeoutTask")
@Slf4j
public class CheckIssueTimeoutTask {

    @Autowired
    private DifficultyDao difficultyDao;

    @Autowired
    private ExpertProjectDao projectDao;

    @Autowired
    private ExpertDao expertDao;
    /**
     * 业务逻辑
     */
    public void reptilian() {
        log.info("checkIssueTimeoutJob start...");
        String currentDate = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD);
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("expiryDate", currentDate);
        List<Difficulty> difficultyList = difficultyDao.pageTimeoutDifficultyList(queryParam);
        if (ObjectUtils.isEmpty(difficultyList)) {
            log.info("exist none timeout difficulty...");
            log.info("checkIssueTimeoutJob end...");
            return;
        }
        List<String> difficultyIdList = difficultyList.stream().map(Difficulty::getId).collect(Collectors.toList());
        difficultyDao.batchUpdateStatus(difficultyIdList, DifficultyStatusEnum.OVERTIME.getValue());
        log.info("checkIssueTimeoutJob end...");
    }

    private void checkParameter() {
        String summaryTime = TimeUtils.getCurrentYearStart();
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("summaryTime", summaryTime);
        List<ExpertProject> projectList = projectDao.getProjectListByTime(queryParam);
        if (!ObjectUtils.isEmpty(projectList)) {
            List<Expert> allExpertList = expertDao.queryExpertList(0);
            Map<String, Map<String, Integer>> expertMap = new HashMap<>();
            for (ExpertProject project : projectList) {
                List<String> expertList = new ArrayList<>();
                List<String> appointedExpertList = Arrays.asList(project.getAppointedExpert().split(","));
                expertList.addAll(appointedExpertList);
                List<String> divideExpertList = Arrays.asList(project.getDivideExpert().split(","));
                expertList.addAll(divideExpertList);
                if (ObjectUtils.isEmpty(expertList))
                    continue;
                for (String exp : expertList) {
                    Map<String, Integer> countMap = expertMap.getOrDefault(exp, new HashMap<>());
                    countMap.put("projectNum", countMap.get("projectNum")+ 1);
                    expertMap.put(exp, countMap);
                }
            }
        }
        List<Difficulty> riskList = this.difficultyDao.pageDifficultyList(queryParam);
    }
}

